package pl.edu.agh.gt;

import java.util.List;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import pl.edu.agh.gt.ga.CrossbreedOperator;
import pl.edu.agh.gt.ga.FitnessFunction;
import pl.edu.agh.gt.ga.Individual;
import pl.edu.agh.gt.ga.Initializer;
import pl.edu.agh.gt.ga.MutationOperator;
import pl.edu.agh.gt.ga.Parameters;
import pl.edu.agh.gt.ga.Population;
import pl.edu.agh.gt.ga.greedy.CrossbreedImlpl;
import pl.edu.agh.gt.ga.greedy.FitnessFunctionImpl;
import pl.edu.agh.gt.ga.greedy.InitializerImpl;
import pl.edu.agh.gt.ga.greedy.MutationImpl;

public class EA {

	private final static Logger log = Logger.getLogger(EA.class);
	private static long start;
	private static long stop;

	public static void main(String[] args) {

		try {
			Logger.getLogger("pl.edu.agh.gt.ea").setLevel(Level.INFO);
			Logger.getLogger("pl.edu.agh.gt.strategy").setLevel(Level.WARN);
			Logger.getLogger("pl.edu.agh.gt.stock").setLevel(Level.WARN);
			
			tic();

			// preare params
			Parameters params = new Parameters();

			FitnessFunction fitness = new FitnessFunctionImpl();
			MutationOperator mutation = new MutationImpl();
			CrossbreedOperator xbreed = new CrossbreedImlpl();
			Initializer initializer = new InitializerImpl();

			// create population
			Population population = new Population(params, initializer,
					fitness, mutation, xbreed);

			// run ...
			population.initializePopulation();

			population.evolvePopulation();

			// present results
			// stock performance for best ind's
			List<Individual> bestInds = population.getNBestIndividuals(3);
			for(Individual i : bestInds)
				((FitnessFunctionImpl)fitness).showGenotypeTradingComparedToBuyAndHold(i.genotype);
			
			tac();
			
		} catch (Exception e) {
			log.error(e);
		}

	}

	private static void tac() {
		stop = System.currentTimeMillis();
		log.info("Time elapsed [s]: "+(stop - start)/1000.0f);
	}

	private static void tic() {
		start = System.currentTimeMillis();
	}

}
